
package com.campus_information_portal.service.impl;
import com.campus_information_portal.dao.MaterialDao;
import com.campus_information_portal.entity.Material;
import com.campus_information_portal.entity.MaterialMaterialTypeTeacher;
import com.campus_information_portal.service.MaterialService;
import com.campus_information_portal.util.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 20155873 张联富
 * @Description $Description$
 * @methodName $methodName$
 * @date $date$ $time$
 * @return $returns$
 */

@Service
public class MaterialImpl implements MaterialService{
    @Autowired
    private MaterialDao materialDao;

    @Override
    public List<Material> findAllMaterial(int material_state) {
        List<Material> list=new ArrayList<Material>();
        System.out.println("++"+material_state);
        list= materialDao.findAllMaterial(material_state);
        return list;
    }

    @Override
    public Message addMaterial(Material material) {
        boolean flag=false;
        flag=materialDao.AddMaterial(material,material.getMaterialId());
        if (flag){
            return Message.success().add("上传成功",1);
        }else {
            return Message.fail();
        }
    }
    /**
     * @Description 更新下载次数
     * @author 20155878 鄢前州
     * @methodName updatematerial_uptime
     * @returnType boolean
     * @create 10:40 2018/5/3
     **/
    @Override
    public Message updatematerial_uptime(Material material) {
        Integer downtimes=materialDao.querymaterial_downtimesBymaterialNme(material.getMaterialName());
        System.out.println("下载次数："+downtimes);
        material.setMaterialDowntimes(downtimes+1);
        materialDao.updatematerial_uptime(material);
        return Message.success();
    }
    /**
     * @Description 根据资料id查询下载次数
     * @author 20155878 鄢前州
     * @methodName querymaterial_downtimesBymaterial_id
     * @returnType int
     * @create 11:45 2018/5/3
     **/
    @Override
    public Integer querymaterial_downtimesBymaterialName(String materialName) {
        int downTimes=materialDao.querymaterial_downtimesBymaterialNme(materialName);
        System.out.println("impl层按id查询资料下载次数成功");
        return downTimes;
    }
    /**
     * @Method_name: querymaterial_type_idsBymaterial_typeName
     * @Describe: 通过文件类型名查抄文件类型ID
     * @Author: 20155878 鄢前州
     * @Creat_date: 19:02
     * @Creat_time: 2018/5/31
     **/
    @Override
    public String querymaterial_type_idsBymaterial_typeName(String materialTpyeName){
        String materialTyeId=materialDao.querymaterial_type_idBymaterial_typName(materialTpyeName);
        if (materialTyeId==null){
            materialDao.AddMaterialType(materialTpyeName,0);
            materialTyeId=materialDao.querymaterial_type_idBymaterial_typName(materialTpyeName);
        }else{
            return materialTyeId;
        }
        System.out.println("转类型后的资料类型ID："+materialTyeId);
        return materialTyeId;
    }
    /**
     * @Describe: 联合老师表资料表和资料类型表进行链表查询全部信息
     * @Author: 20155878 鄢前州
     * @Creat_date: 22:23
     * @Creat_time: 2018/6/1
     **/
    @Override
    public Message queryAllMaterial(){
        List<MaterialMaterialTypeTeacher> list = materialDao.queryAllMaterial();
        if (list.size() > 0) {
            return Message.success().add("materialMaterialTypeTeacher", list);
        }
        return Message.fail();
    }
    /**
     * @Method_name: addMaterialType
     * @Describe: 添加资料类型
     * @Author: 20155878 鄢前州
     * @Creat_date: 23:50
     * @Creat_time: 2018/6/1
     **/
    @Override
    public Message addMaterialType(String materialTypeName){
        Boolean flag=materialDao.AddMaterialType(materialTypeName,0);
        if (flag){
            return Message.success();
        }
        return Message.fail();
    }
}
